﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MVCWeb.Models
{
    public class OrderItemModel
    {
        [Required]
        [DisplayName("Order Id")]
        public int order_id { get; set; }

        [Required]
        [DisplayName("Product Id")]
        public int product_id { get; set; }

        [Required]
        [DisplayName("Quantity")]
        public int quantity { get; set; }

        [Required]
        [DisplayName("Subtotal")]
        public double sub_total { get; set; }

        [Required]
        [DisplayName("Item State")]
        public int item_state { get; set; }
    }

    public static class OrderItemClientService
    {
        public static void InsertOrderItems(OrderItemModel order)
        {
            SLOrderItems.OrderItem newOrder = DTO_to_SL(order);

            SLOrder.ISLOrder SLOrder = new SLOrder.SLOrderClient();
            SLOrderItems.ISLOrderItems SLOrderItems = new SLOrderItems.SLOrderItemsClient();

            string[] errors = new string[0];
            SLOrderItems.InsertOrderItems(newOrder, ref errors);
        }

        public static void DeleteOrderItems(int oid, int pid)
        {
            SLOrderItems.ISLOrderItems SLOrderItems = new SLOrderItems.SLOrderItemsClient();
            string[] errors = new string[0];
            SLOrderItems.DeleteOrderItem(oid, pid, ref errors);
        }

        /*public static int NumOrderItems()
        {
            SLOrderItems.ISLOrderItems SLOrderItems = new SLOrderItems.SLOrderItemsClient();

            string[] errors = new string[0];
            int numItems = SLOrderItems.NumOrderItems(ref errors);
                
            //OrderItemModel order = DTO_to_PL(

            return numItems;
        }*/
        //GetOrderProducts
        public static List<OrderItemModel> GetOrderProducts(int id)
        {
            List<OrderItemModel> orderitemlist = new List<OrderItemModel>();
            SLOrderItems.ISLOrderItems SLOrderItem = new SLOrderItems.SLOrderItemsClient();

            string[] errors = new string[0];

            SLOrderItems.OrderItem[] orderitem = SLOrderItem.GetOrderProducts(id, ref errors);


            if (orderitem != null)
            {
                foreach (SLOrderItems.OrderItem oi in orderitem)
                {

                    OrderItemModel item = DTO_to_PL(oi);
                    orderitemlist.Add(item);
                }
            }

            return orderitemlist;

        }

        //GetOrderProducts
        public static List<OrderItemModel> SeeMyHistory(int id)
        {
            List<OrderItemModel> orderitemlist = new List<OrderItemModel>();
            SLOrderItems.ISLOrderItems SLOrderItem = new SLOrderItems.SLOrderItemsClient();

            string[] errors = new string[0];

            SLOrderItems.OrderItem[] orderitem = SLOrderItem.SeeMyHistory(id, ref errors);


            foreach (SLOrderItems.OrderItem oi in orderitem)
            {

                OrderItemModel item = DTO_to_PL(oi);
                orderitemlist.Add(item);
            }

            return orderitemlist;

        }

        public static OrderItemModel DTO_to_PL(SLOrderItems.OrderItem order_item)
        {
            OrderItemModel OrderItemModel = new Models.OrderItemModel();
            OrderItemModel.order_id = order_item.order_id;
            OrderItemModel.product_id = order_item.product_id;
            OrderItemModel.quantity = order_item.quantity;
            OrderItemModel.sub_total = order_item.sub_total;
            OrderItemModel.item_state = order_item.item_state;

            return OrderItemModel;
        }

        public static SLOrderItems.OrderItem DTO_to_SL(OrderItemModel order)
        {
            SLOrderItems.OrderItem SLOrderItems = new MVCWeb.SLOrderItems.OrderItem();

            SLOrderItems.order_id = order.order_id;
            SLOrderItems.product_id = order.product_id;
            SLOrderItems.quantity = order.quantity;
            SLOrderItems.sub_total = order.sub_total;
            SLOrderItems.item_state = order.item_state;

            return SLOrderItems;
        }
    }
}